﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Net.Mime;
using DAL;
using System.Collections.Generic;
using System.ComponentModel;
using Telerik.Web.UI;
using System.Globalization;



public partial class Cost_UserControl_uc_ChiPhi_NhapHang_View : System.Web.UI.UserControl
{
    public int _MasterID;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            InitData();
        }
    }

    private void InitData()
    {
        lblAlerting.Text = string.Empty;
        hdfMasterID.Value = _MasterID.ToString();
        GetMasterData(_MasterID);
        Search(_MasterID);
    }
    public void GetMasterData(int Id)
    {
        lblAlerting.Text = string.Empty;
        CostBO pur = new CostBO();
        ZD_PRC_ZD_COST_GETBY_IDResult result = new ZD_PRC_ZD_COST_GETBY_IDResult();
        result = pur.Cost_GetByMasterId(Id);
        txtSoVanDon.Text = result.SOVANDON;

    }

    public void Search(int Id)
    {
        lblAlerting.Text = string.Empty;
        CostBO pur = new CostBO();
        List<ZD_PRC_CHIPHI_GETResult> result = new List<ZD_PRC_CHIPHI_GETResult>();
        result = pur.SearchChiPhi(Id);
        grdList.DataSource = result;
        grdList.DataBind();

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            CostBO pur = new CostBO();
            if (txtSoVanDon.Text.Trim().Length <= 0)
            {
                lblAlerting.Text = "Bạn chưa nhập số vận đơn!";
                return;
            }
           
            bool check = false;
            if (grdList.Rows.Count > 0)
            {
                for (int i = 0; i < grdList.Rows.Count; i++)
                {
                    // Cell 0 la checkbox
                    CheckBox chkSave = ((CheckBox)grdList.Rows[i].Cells[0].FindControl("chkSave"));
                    if (chkSave.Checked)
                    {
                        check = true;
                        break;


                    }
                }
                if (!check)
                {
                    lblAlerting.Text = "Không có dữ liệu!";
                    return;
                }

            }
            else
            {
                lblAlerting.Text = "Không có dữ liệu!";
                return;
            }
            string strDSID = string.Empty;

            for (int i = 0; i < grdList.Rows.Count; i++)
            {
                // Cell 0 la checkbox
                CheckBox chkSave = ((CheckBox)grdList.Rows[i].Cells[0].FindControl("chkSave"));
                if (chkSave.Checked)
                {
                    string strID = ((HiddenField)grdList.Rows[i].Cells[0].FindControl("hdfID")).Value;
                    strDSID += strID + "@";


                }
            }
             if (int.Parse( hdfMasterID.Value) > 0)
            {
                // save detail
                ZD_COST_BELONG objBelong = new ZD_COST_BELONG();
                objBelong.MASTERID = int.Parse(hdfMasterID.Value);
                objBelong.CREATEUSER = int.Parse(Session["UserID"].ToString());
                int rs = pur.Cost_BelongInsert(objBelong, strDSID);
                if (rs > 0)
                {
                   // string Link = "../cost/cprpt_" + hdfMasterID.Value;
                   // Response.Redirect(Link);
                    lblAlerting.Text = "Bạn cập nhật dữ liệu thành công!";
                    return;
                }
                else
                {
                    lblAlerting.Text = "Cập nhật dữ liệu thất bại, bạn vui lòng thử lại sau!";
                    return;
                }
            }
            else
            {
                lblAlerting.Text = "Có lỗi xảy ra bạn vui lòng thử lại sau!";
                return;
            }
        }

        catch (Exception ex)
        {


        }
    }

}
